<?php

namespace app\widgets;

use yii\widgets\InputWidget;
use yii\helpers\Html;
use yii\helpers\Json;
/**
 * 文本编辑器小部件
 * Class EditorWidget
 * @package app\widgets
 */
class Editor extends InputWidget
{
    /**
     * @var array the HTML attributes for the input tag.
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
     */
    public $options = ['class' => 'form-control','onkeydown'=>'ajaxupload(events)'];

    /**
     * @var array 返回一个文本编辑器设置
     */
    public $editorOptions = [];
    /**
     * @var string the template for arranging the CAPTCHA image tag and the text input tag.
     * In this template while `{input}` will be replaced with the text input tag.
     */
    public $template = '{input}';


    public function init()
    {
        parent::init();

        if (!isset($this->editorOptions['id'])) {
            $this->editorOptions['id'] = $this->options['id'] ;
        }
    }
    public function run()
    {
        $this->registerClientScript();
        if ($this->hasModel()) {
            $input = Html::activeTextarea($this->model, $this->attribute, $this->options);
        } else {
            $input = Html::textarea($this->name, $this->value, $this->options);
        }
        echo strtr($this->template, [
            '{input}' => $input,
        ]);
    }

    /**
     * Registers the needed JavaScript.
     */
    public function registerClientScript()
    {
        $options = $this->getClientOptions();
        $options = empty($options) ? '' : Json::htmlEncode($options);
        $id = $this->editorOptions['id'];
        $view = $this->getView();
        EditorAsset::register($view);
        $view->registerJs("
        function uploadfile(){

        }
        KindEditor.ready(function(K) {
            var kindConfig = $options;
            kindConfig.afterBlur = function(){
                this.sync();
            };
            editor = K.create('#$id',kindConfig);
        });
        prettyPrint();

        ");
    }

    /**
     * Returns the options for the JS widget.
     * @return array the options
     */
    protected function getClientOptions()
    {
        $options = [
            'resizeType'=>0,
            'width'=> '100%',
	        'height'=> '1000px',
            'afterCreate'=>'',
        ];

        return $options;
    }
}